package io.gatling.extensions.mysql.builder

import com.typesafe.scalalogging.StrictLogging
import io.gatling.core.action.Action
import io.gatling.core.action.builder.ActionBuilder
import io.gatling.core.session.Expression
import io.gatling.core.structure.ScenarioContext
import io.gatling.extensions.mysql.action.MysqlAction
import io.gatling.extensions.mysql.protocol.MysqlProtocol

case class MysqlActionBuilder(name: String) extends ActionBuilder with StrictLogging {

  var operate: MysqlOperate = _

  def select(sql: Expression[String]): MysqlActionBuilder = {
    operate = Select(sql)
    this
  }

  def update(sql: Expression[String]): MysqlActionBuilder = {
    operate = Update(sql)
    this
  }

  def delete(sql: Expression[String]): MysqlActionBuilder = {
    operate = Delete(sql)
    this
  }

  def insert(sql: Expression[String]): MysqlActionBuilder = {
    operate = Insert(sql)
    this
  }

  override def build(ctx: ScenarioContext, next: Action): Action = {
    import ctx._
    MysqlAction(
      name,
      operate,
      protocolComponentsRegistry.components(MysqlProtocol.mysqlProtocolKey),
      coreComponents,
      throttled,
      next
    )
  }
}

sealed trait MysqlOperate {
  def sql: Expression[String]
}

case class Select(sql: Expression[String]) extends MysqlOperate

case class Update(sql: Expression[String]) extends MysqlOperate

case class Delete(sql: Expression[String]) extends MysqlOperate

case class Insert(sql: Expression[String]) extends MysqlOperate
